﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoResumeSend.Entity;
using System.IO;
using NLog;

namespace AutoResume
{
    public partial class FrmSend : Form
    {
        Logger m_log = LogManager.GetLogger("sys_log");
        int qq_number = 100000000;
        public FrmSend()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(textBox1.Text.Trim(), out qq_number))
                qq_number = 100000000;
            int index = 0;
            while (qq_number > 5000)
            {

               bool t= SendEmail();

               if (t)
               {
                   m_log.Info(string.Format("成功发送：{0}", qq_number.ToString()));
                  
               }
               qq_number = qq_number - 1;
                index ++;

                if (index % 20 == 0)
                {
                    //停10分钟
                    System.Threading.Thread.Sleep(10 * 60 * 1000);
                }
            }
        }

        private bool SendEmail()
        {
            try
            {

                //发送邮件
                MailInfo mail = new MailInfo();
                mail.Subject = "大众小说网热门小说推介";

                mail.From = "dazhongxiaoshuo@126.com";
                mail.To = string.Format("{0}@qq.com", qq_number);
                mail.FromDisplayName = "dazhongxiaoshuo";
                string bodyPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ad.htm");

                string body = File.ReadAllText(bodyPath, System.Text.Encoding.UTF8);
                mail.Body = body;

                AutoResumeSend.Componments.SendMailManager mailMgr = new AutoResumeSend.Componments.SendMailManager();
                mailMgr.sendMail(mail, "smtp.126.com", 25, "dazhongxiaoshuo", "2004111091");
                return true;
            }
            catch (Exception ex)
            {
                m_log.Error(ex.ToString());
                ex = null;
                return false;
            }
        }
    }
}
